﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FacebookWrapper;
using FacebookWrapper.ObjectModel;
using Facebook;

namespace AutomaticAdvertising
{
    public class Publisher
    {
        private User m_LoggedInUser;
        private string m_AccessToken;

        public Publisher()
        {
        }
        
        public User Login()
        {
            LoginResult result = FacebookService.Login(
                "597589053603558",
                "friends_about_me",
                "status_update",
                "user_photos",
                "read_stream",
                "user_activities",
                "create_event",
                "publish_actions",
                "user_birthday",
                "publish_stream",
                "read_mailbox",
                "read_friendlists",
                "friends_about_me",
                "friends_birthday",
                "friends_relationship_details",
                "friends_location", 
                "friends_hometown", 
                "friends_likes",
                "friends_website",
                "friends_status",
                "friends_checkins",
                "user_location", 
                "email", 
                "user_birthday", 
                "offline_access", 
                "user_hometown");

            if (!string.IsNullOrEmpty(result.AccessToken))
            {
                m_LoggedInUser = result.LoggedInUser;
                m_AccessToken = result.AccessToken;
            }
            else if (!string.IsNullOrEmpty(result.ErrorMessage))
            {
                throw new System.Exception(result.ErrorMessage);
            }
            else
            {
                throw new System.Exception("Unknown error message during login.\nPlease try again");
            }

            return m_LoggedInUser;
        }

        public User Refresh()
        {
            LoginResult result = FacebookService.Connect(m_AccessToken);

            if (string.IsNullOrEmpty(result.ErrorMessage))
            {
                m_LoggedInUser = result.LoggedInUser;
            }
            else
            {
                throw new System.AggregateException(result.ErrorMessage);
            }

            return m_LoggedInUser;
        }

        public Post PostAdvertisement(PostData postData)
        {
            Post postedPost;

            postedPost = m_LoggedInUser.PostPhoto(postData.ImageLocalPath, postData.Text);

            return postedPost;
        }

        public void PostOnFriensWall(string i_Message)
        {
            FacebookClient app = new FacebookClient(m_AccessToken);
            
            app.Post("/me/feed", new Dictionary<string, object> { { "message", i_Message } });
            
            /*https://developers.facebook.com/roadmap/completed-changes/
             * Removing ability to post to friends walls via Graph API 
             * We will remove the ability to post to a user's friends' 
             * walls via the Graph API. Specifically, posts against 
             * [user_id]/feed where [user_id] is different from the session 
             * user, or stream.publish calls where the target_id user is 
             * different from the session user, will fail. If you want 
             * to allow people to post to their friends' timelines, 
             * invoke the feed dialog. Stories that include friends 
             * via user mentions tagging or action tagging will show up on the 
             * friend’s timeline (assuming the friend approves the tag). 
             * For more info, see this blog post.*/
        }

        public bool DeletePostAdvertisement(PostData i_PostData)
        {
            bool isSuccess = false;
   
            if (!string.IsNullOrEmpty(i_PostData.Id))
            {
                FacebookClient facebookClient = new FacebookClient(m_AccessToken);

                facebookClient.Delete(i_PostData.Id);

                isSuccess = true;
            }

            return isSuccess;
        }
    }
}
